//
//  AddBirthViewController.m
//  FD
//
//  Created by Yasin on 2018/10/17.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//
//添加成员生日
#import "AddBirthViewController.h"
#import "BRDatePickerView.h"

//view
#import "BRInfoCell.h"
#import "AddBirthHeaderView.h" //

@interface AddBirthViewController ()<UITableViewDelegate,UITableViewDataSource,
UITextFieldDelegate>
/**tv*/
@property (nonatomic,  strong) UITableView *tv;
/**生日：时间戳 */
@property (nonatomic,  strong) NSNumber *timestamp;

/**表头*/
@property (nonatomic, strong)  AddBirthHeaderView *headerView;
/**表尾*/
@property (nonatomic,  strong) UIView *footerView;

@end

@implementation AddBirthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(AddFamilymember, @"添加成员生日") ;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加view
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    
    self.tv.separatorStyle = UITableViewCellEditingStyleNone;
    
    self.tv.tableHeaderView = self.headerView;
    
    self.footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
    self.tv.tableFooterView = self.footerView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    return cell;
}
#pragma mark  - 点击事件
- (void)clickAddBtnAction:(UIButton *)btn
{
    NSString *name = self.headerView.pleaseInputNameTF.text;
    
    NSLog(@"%s----%d",__FUNCTION__,__LINE__);
    if ([NSString isEmptyString:name]) {
//        NSString *tips1 = NSLocalizedStringFromTable(Tips_PutYouName, @"Localizable", @"请输入姓名");
        [JRToast showWithText:NSLocalizedString(Home_put_your_contacts_name, @"请输入姓名")];

        return;
    }
    
    if ([NSString isEmptyString:[NSString stringWithFormat:@"%@",self.timestamp]]) {
        [JRToast showWithText:NSLocalizedString(ToastPleaseSelectDate, @"请选择日期")];
        return;
    }
    
    FDLog(@"时间戳:%@",self.timestamp);
    //调接口
    [FDHttpAPi requestUserFamilSaveName:name birthdaystamp:self.timestamp successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            [JRToast showWithText:NSLocalizedString(ToastAddSucess, @"添加成功")];
            
            [self.navigationController popViewControllerAnimated:YES];
            //返回
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}


#pragma mark - getter 放最后
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.delegate = self;
        _tv.dataSource = self;
        _tv.backgroundColor = kMainLightGrayColor;
        //注册cell、
        [_tv registerClass:[BRInfoCell class] forCellReuseIdentifier:@"BRInfoCell"];
    }
    return _tv;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] init];
        _footerView.backgroundColor = kMainLightGrayColor;
        
        //添加按钮
        UIButton *addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        NSString *complectionStr = NSLocalizedString(Completed, @"完成");
        [addBtn setTitle:complectionStr forState:(UIControlStateNormal)];
        [addBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        addBtn.backgroundColor = kMainRedColor;
        [addBtn addTarget:self action:@selector(clickAddBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        addBtn.frame = CGRectMake(20, 50, SCREEN_WIDTH - 40, 40);
        addBtn.layer.masksToBounds = YES;
        addBtn.layer.cornerRadius = 5;
        
        //添加
        [_footerView addSubview:addBtn];
    }
    return _footerView;
}

- (AddBirthHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[AddBirthHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        
        FDWeakSelf;
        //事件回调
        
        
        //block里面一般都使用self增强语境。
//        Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior Insert   'self->'
        //block隐式持有 'self' ,显示添加 'self' 增强语境,这是有意为之的。

//        Capturing 'self' strongly in this block is likely to lead to a retain cycle
        _headerView.headerViewSelectBirthDayBlock = ^{
            //回收键盘
            [weakSelf.headerView.pleaseInputNameTF resignFirstResponder]; //回收键盘
            
            NSDate *minDate = [NSDate br_setYear:1900 month:1 day:1];
            NSDate *maxDate = [NSDate date];
            //国际化
            NSString *csrqStr = NSLocalizedString(DateOfBirth, @"出生日期");
            
            [BRDatePickerView showDatePickerWithTitle:csrqStr dateType:BRDatePickerModeYMD defaultSelValue:@"2000-1-1" minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {

                //wea
                weakSelf.headerView.pleaseBirthDayLb.text = selectValue;
                
                //时间戳
                NSNumber *timeStampxxx = [NSString timeStampFromDateStr:selectValue];
                
                weakSelf.timestamp = timeStampxxx; //保存时间戳
                
            } cancelBlock:^{
                NSLog(@"点击了背景或取消按钮");
            }];
        };
    }
    return _headerView;
}
@end

